/*
 * @(#)AttributeTransformer.java
 * Date 2013-09-26
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.registry.transformer.model;

import oasis.names.tc.ebxml_regrep.xsd.rim._3.SlotType1;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import us.hornerscorners.lamppost.exception.ServiceActionError;
import us.hornerscorners.lamppost.exception.ServiceActionException;
import us.hornerscorners.lamppost.exception.SeverityType;
import us.hornerscorners.lamppost.exception.ValidationException;

import us.hornerscorners.lamppost.model.document.Attribute;

import us.hornerscorners.lamppost.registry.constant.ErrorCode;

import us.hornerscorners.lamppost.registry.utils.XDSUtils;

import us.hornerscorners.lamppost.transformer.AbstractTransformer;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-09-26
 * @author         Jim Horner
 */
public class AttributeTransformer extends AbstractTransformer<SlotType1, Attribute> {

    /** Field description */
    private final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * Method description
     *
     *
     *
     * @return
     *
     * @throws ServiceActionException
     */
    @Override
    public Attribute transform(SlotType1 slot) throws ServiceActionException {

        Attribute result = null;

        String strSlotName = slot.getName();

        if (XDSUtils.isValidExtraSlotName(strSlotName)) {

            logger.warn("Unknown slot name: {}. Creating extraAttribute.", strSlotName);

            // store as extra attribute
            result = new Attribute();

            result.setName(strSlotName);
            result.getValues().addAll(XDSUtils.parseValues(slot));

        } else {

            String msg =
                String.format(
                    "Extra metadata slot name [%s] does not start with 'urn:' or starts with 'urn:ihe:'.",
                    strSlotName);

            ServiceActionError error =
                new ServiceActionError(ErrorCode.XDSRegistryMetadataError.getCode(), msg,
                                       SeverityType.Error);

            throw new ValidationException(getErrorLocation(), error);
        }

        return result;
    }
}
